//-----------------------------------------------------------------------
// <copyright file="SupplierDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class SupplierDAOBase
    {
        #region Common methods
        public virtual Supplier CreateSupplierFromReader(IDataReader reader)
        {
            Supplier item = new Supplier();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SupplierCode"))) item.SupplierCode = (string)reader["SupplierCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("SupplierName"))) item.SupplierName = (string)reader["SupplierName"];
                if (!reader.IsDBNull(reader.GetOrdinal("ConPointID"))) item.ConPointID = (int)reader["ConPointID"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsArchived"))) item.IsArchived = (bool)reader["IsArchived"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateSupplierFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateSupplier methods
            
        public virtual void CreateSupplier(Supplier supplier)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spSuppliersInsert");
                
                dbCommandWrapper.AddInParameter("@SupplierCode", DbType.AnsiString, supplier.SupplierCode);
                dbCommandWrapper.AddInParameter("@SupplierName", DbType.String, supplier.SupplierName);
                dbCommandWrapper.AddInParameter("@ConPointID", DbType.Int32, supplier.ConPointID);
                dbCommandWrapper.AddInParameter("@IsArchived", DbType.Boolean, supplier.IsArchived);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateSupplierException, ex);
            }
        }

        #endregion

        #region UpdateSupplier methods
        
        public virtual void UpdateSupplier(Supplier supplier)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spSuppliersUpdate");            
                
                dbCommandWrapper.AddInParameter("@SupplierCode", DbType.AnsiString, supplier.SupplierCode);
                dbCommandWrapper.AddInParameter("@SupplierName", DbType.String, supplier.SupplierName);
                dbCommandWrapper.AddInParameter("@ConPointID", DbType.Int32, supplier.ConPointID);
                dbCommandWrapper.AddInParameter("@IsArchived", DbType.Boolean, supplier.IsArchived);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateSupplierException, ex);
            }
        }
        
        #endregion

        #region DeleteSupplier methods
        public virtual void DeleteSupplier(string supplierCode)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spSuppliersDelete");
                
                dbCommandWrapper.AddInParameter("@SupplierCode", DbType.AnsiString, supplierCode);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteSupplierException, ex);
            }
        }

        #endregion

        #region GetSupplier methods
        
        public virtual Supplier GetSupplier(string supplierCode)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spSuppliersGet");
                
                dbCommandWrapper.AddInParameter("@SupplierCode", DbType.AnsiString, supplierCode);
                
                Supplier supplier =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        supplier = CreateSupplierFromReader(reader);
                        reader.Close();
                    }
                }
                return supplier;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetSupplierException, ex);
            }
        }
        
        #endregion

        #region GetSuppliers methods
        public virtual SupplierCollection GetSuppliers(SupplierColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spSuppliersGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                SupplierCollection supplierCollection = new SupplierCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        Supplier supplier = CreateSupplierFromReader(reader);
                        supplierCollection.Add(supplier);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return supplierCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetSuppliersException, ex);
            }
        }
        
        public virtual SupplierCollection GetSuppliers(SupplierColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetSuppliers(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}